package com.rainner.app.action;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.app.base.action.JsonAction;
import com.rainner.app.server.ReaderServer;

@Results({ 
	@Result(name = "REGISTER", type = "freemarker",location = "/WEB-INF/content/public/register.ftl"),
	@Result(name = "INDEX", type = "redirect", location = "/") })
public class RegisterAction extends JsonAction {
	private static final long serialVersionUID = 1L;
	private String us;
	private String email;
	private String password1;
	private String csny;
	private String shouji;
	private String remark;
	private String xb_one;
	private ReaderServer server = new ReaderServer();

	@Action("/register/checkusername")
	public String registerCheckUsername() throws SQLException {
		this.result.setState(server.checkUsername(us));
		map.put("message", this.result);
		return MAP;
	}

	@Action("/register/checkemail")
	public String registerCheckEmail() throws SQLException {
		this.result.setState(server.checkEmail(email));
		map.put("message", this.result);
		return MAP;
	}

	@Action("/register/registerdata")
	public String registerReader() throws SQLException, IOException {
		Date date = new Date();
		String currentDate = (new SimpleDateFormat("yyyy-MM-dd")).format(date);
		String readerSex = "男";
		if (xb_one.equals("1")) {
			readerSex = "男";
		} else {
			readerSex = "女";
		}
		if (server.addReaderData(us, password1, remark, readerSex, csny, shouji, email, 0, currentDate)) {
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("username", us);
			return "INDEX";
		} else {
			return null;
		}
	}

	@Action("/register")
	public String register() {
		return "REGISTER";
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setUs(String us) {
		this.us = us;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public void setCsny(String csny) {
		this.csny = csny;
	}

	public void setShouji(String shouji) {
		this.shouji = shouji;
	}

	public void setServer(ReaderServer server) {
		this.server = server;
	}

	public void setXb_one(String xb_one) {
		this.xb_one = xb_one;
	}

}
